﻿
using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.View;
using HuiZe.education.ViewModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

namespace HuiZe.education
{
    public class HuiZeeducation : ViewModelBase
    {
    
        /// <summary>
        /// 判断是否在上课中
        /// </summary>
        public bool Classing { get; set; }
        public List<ClassModel> Classes { get; set; } //年级
        public List<CategoryModel> Categorys { get; set; }  //类别
        private static HuiZeeducation instance;
        private static object _lock = new object();
        public string LogoUrl { get; set; }
        private HuiZeeducation()
        {
            Url = CommonMethods.GetAppConfigValue("url");
        }

        public static HuiZeeducation Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (_lock)
                    {
                        if (instance == null)
                        {
                            instance = new HuiZeeducation();
                        }
                    }
                }
                return instance;
            }
        }

        public Userinfo UserInfo { get; set; }
        public string Url { get; set; }
        public void StartHuiZeeducation()
        {
            LoginView loginView = new LoginView();
            loginView2 = loginView;
            loginView.Show();

        }
        public List<ClassModel3> DownLoadClassData2(int id, string str)
        {
            ReqClassInfoModel reqClassInfoModel = new ReqClassInfoModel()
            {
                token = UserInfo.token,
                id = id,
            };
            string data = JsonHelper.ToJson(reqClassInfoModel);
            var result = Request<List<ClassModel3>>("/api/grades/index", data, str);
            if (result.Item1)
                return result.Item2.data;
            else
                return null;
        }
        //public void DownLoadData()
        //{
        //    MainViewModel mainViewModel = CommonServiceLocator.ServiceLocator.Current.GetInstance<MainViewModel>();
        //    mainViewModel.OpenMainView();
        //    DownLoadClassData();
        //    DownLoadCategory();
        //}

        //下载班级列表
        public void DownLoadClassData()
        {
            List<ClassModel> temp = DownLoadClassData(0, "获取年级列表");
            if (temp != null)
            {
                Classes = temp;
                foreach (var item in Classes)
                {
                    if (temp != null)
                    {
                        temp = DownLoadClassData(item.id, "获取年级列表");
                        item.SubClass = temp;
                    }
                }
            }
        }
        MainViewModel mainViewModel2 { get; set; }
        LoginView loginView2 { get; set; }
        OtherLoginView OtherLoginView { get; set; }
        public void RestartHuiZeeducation()
        {
            if (mainViewModel2 != null)
            {
                // 遍历并关闭所有子窗口
                //foreach (Window item in Application.Current.Windows)
                //{
                //    if (item.Title == "MainWindow")
                //    {
                //        item.Content = null;


                //    }
                
                //}

                mainViewModel2.CloseMainView();
                mainViewModel2.ClearView();
                //LoginView loginView = new LoginView();
                loginView2.Show();
            }




        }
        public void OtherLoginHuiZeeducation()
        {
            if (mainViewModel2 != null)
            {

                mainViewModel2.CloseMainView();
                //LoginView loginView = new LoginView();
                //OtherLoginView.Show();
            }




        }
        public void DownLoadData()
        {
            MainViewModel mainViewModel = CommonServiceLocator.ServiceLocator.Current.GetInstance<MainViewModel>();
            mainViewModel.OpenMainView();
            DownLoadClassData();
            DownLoadCategory();
            mainViewModel2 = mainViewModel;
        }

        public List<ClassModel> DownLoadClassData(int id, string str)
        {
            ReqClassInfoModel reqClassInfoModel = new ReqClassInfoModel()
            {
                token = UserInfo.token,
                id = id,
            };
            string data = JsonHelper.ToJson(reqClassInfoModel);
            var result = Request<List<ClassModel>>("/api/grades/classlist", data, str);
            if (result.Item1)
                return result.Item2.data;
            else
                return null;
        }

        public void DownLoadCategory()
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs["token"] = UserInfo.token;
            Debug.WriteLine($"类别列表token：{UserInfo.token}");
            string data = JsonHelper.ToJson(keyValuePairs);
            var result = Request<List<CategoryModel>>("/api/resources/getKindList", data, "获取类列表");
            if (result.Item1)
            {
                Categorys = result.Item2.data;
            }
        }


    }
}
